#region using

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

#endregion

[ExtensionAttribute("Applies a Sepia Tone to photos", "1.0.0.0", "Phil Garcia")]
public class SepiaTone : IPhotoEffect
{
	static private PhotoEffectSettings _settings = new PhotoEffectSettings();

	public SepiaTone()
	{
		PhotoEffectCollection.Register("Sepia Tone", this);
	}

	public PhotoEffectSettings GetSettings()
	{
		return _settings;
	}

	public System.Drawing.Image Apply(System.Drawing.Image source, PhotoEffectParameters parameters)
	{
		return ApplySepiaTone(source);
	}
	/// <summary>
	/// Converts the image to negative counterpart.
	/// </summary>
	/// <param name="original">The original image.</param>
	/// <returns></returns>
	public static Bitmap ApplySepiaTone(System.Drawing.Image original)
	{
		// create a blank bitmap the same size as original
		Bitmap newBitmap = new Bitmap(original.Width, original.Height);

		// get a graphics object from the new image
		Graphics g = Graphics.FromImage(newBitmap);

		ColorMatrix colorMatrix = new ColorMatrix(new float[][] 			  
			{
				new float[] {0.393f, 0.349f, 0.272f, 0, 0},
				new float[] {0.769f, 0.686f, 0.534f, 0, 0},
				new float[] {0.189f, 0.168f, 0.131f, 0, 0},
		        new float[] {     0,      0,      0, 1, 0},
				new float[] {     0,      0,      0, 0, 1}
			});

		// create some image attributes
		ImageAttributes attributes = new ImageAttributes();

		// set the color matrix attribute
		attributes.SetColorMatrix(colorMatrix);

		// draw the original image on the new image using the grayscale color matrix
		g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

		// dispose the Graphics object
		g.Dispose();

		return newBitmap;
	}
}
